package com.bookmarket.controller;

import com.bookmarket.entity.Book;
import com.bookmarket.entity.Stype;
import com.bookmarket.service.AddBookService;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * manyfile
 *
 * @author 李亮
 * @date 2018/1/11
 */
@Controller
@RequestMapping("/file")
public class LLmanyfile {

    @Resource
    private AddBookService addbookService;
    @RequestMapping("/addfile")
    public ModelAndView addBook(@RequestParam CommonsMultipartFile files[], @RequestParam MultipartFile excel, HttpSession session) throws IllegalStateException, IOException {
        ModelAndView mv=new ModelAndView();
        //创建文件保存路径
        String path = session.getServletContext().getRealPath("/uploads");
        for (int i = 0; i < files.length; i++) {
            String fileName = files[i].getOriginalFilename();
            File file = new File(path, fileName);
            files[i].transferTo(file);
        }
        /*System.out.println("文件上传完成");*/
        try {
            InputStream is = excel.getInputStream();
            //得到excel
            Workbook workbook = Workbook.getWorkbook(is);
            //得到sheet
            Sheet sheet = workbook.getSheet(0);
            //得到列数
            int colsNum = sheet.getColumns();
            //得到行数
            int rowsNum = sheet.getRows();
            //单元格
            Cell cell;

            for (int i = 1; i < rowsNum; i++){//我的excel第一行是标题,所以 i从1开始

                   String name= sheet.getCell(0, i).getContents();
                    String author= sheet.getCell(1, i).getContents();
                    String publish= sheet.getCell(2, i).getContents();
                    String publish_date= sheet.getCell(3, i).getContents();
                    int stock=  Integer.parseInt(sheet.getCell(4, i).getContents());
                    int price=  Integer.parseInt(sheet.getCell(5, i).getContents());
                    String picture= sheet.getCell(6, i).getContents();
                    int click=  Integer.parseInt(sheet.getCell(7, i).getContents());
                    String content= sheet.getCell(8, i).getContents();
                    String file= sheet.getCell(9, i).getContents();
                   int stypeid= Integer.parseInt(sheet.getCell(10, i).getContents());
                 Stype stype=new Stype(stypeid);

                Book book=new Book(name,author,publish,publish_date,stock,price,picture,click,content,file,stype);
                /*System.out.println("导入"+book.getName());*/
                addbookService.addBook(book);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } catch (BiffException e) {
            e.printStackTrace();
        }
    mv.setViewName("redirect:/backstage/LAQBook.ftl");
        return mv;
    }



    public AddBookService getAddBookService() {
        return addbookService;
    }

    public void setAddBookService(AddBookService addbookService) {
        this.addbookService = addbookService;
    }

}